package me.xcmc.org.command;

import me.xcmc.org.StarsItem;
import me.xcmc.org.api.Owner;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.util.Objects;

public class Cmd implements CommandExecutor {
    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        if (sender.isOp()){
            if (args.length >= 1){
                if (args[0].equalsIgnoreCase("reload")){
                    StarsItem.getPlugin(StarsItem.class).LoadConfig();
                    sender.sendMessage(StarsItem.prefix + "§e插件重载完毕！");
                }else if (args.length >= 4){
                    if (args[0].equalsIgnoreCase("give")){
                        if (Bukkit.getPlayer(args[1]) != null){
                            ItemStack is = Owner.getItem(args[2]);
                            if (is != null){
                                is.setAmount(Integer.parseInt(args[3]));
                                Objects.requireNonNull(Bukkit.getPlayer(args[1])).getInventory().addItem(is);
                                Objects.requireNonNull(Bukkit.getPlayer(args[1])).sendMessage(StarsItem.prefix + "§e你收到了一件装备！");
                                sender.sendMessage(StarsItem.prefix + "§e成功给予物品！");
                            }else {
                                sender.sendMessage(StarsItem.prefix + "§c没有这个物品！");
                            }
                        }else {
                            sender.sendMessage(StarsItem.prefix + "§c玩家不在线！");
                        }
                    }else {
                        help(sender,label);
                    }
                }else {
                    help(sender,label);
                }
            }else {
                help(sender,label);
            }
        }else {
            Player p = (Player) sender;
            p.performCommand("Sadasdsada");
        }
        return false;
    }
    public void help(CommandSender sender,String label){
        sender.sendMessage(StarsItem.prefix + "§e/" + label + " reload §a重载插件配置");
        sender.sendMessage(StarsItem.prefix + "§e/" + label + " give §b<玩家> <物品> <数量> §a重载插件配置");
    }
}
